Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

275
Views
How to sort String from alphabet first then number (in javascript). example if the input is "asd5lk43fj6vc" and the output should be "acdfjklsv3456"

I'm working on a simple sorting program where i need to sort an input from a user to remove whitespace, special character and sort the string(from user input) ascendingly. im new to learning js and i found this code online.

let sortNumButton = document.getElementById('sortNumButton');
let sortOutputContainer = document.getElementById('sortOutputContainer');
let inputField = document.getElementById('inputField');

sortNumButton.addEventListener('click', function(){
let paragraph = document.createElement('p')

paragraph.innerText = inputField.value.split('').sort().join('').replace(/[^a-zA-Z0-9-. ]/g, "");
sortOutputContainer.appendChild(paragraph);
inputField.value = "";
})

the program works but the output prints the number first, not alphabet first

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can .replace the result afterwards, matching numeric characters and putting that capture group at the end instead of the beginning.

const inputValue = "asd5lk43fj6vc";
const sorted = [...inputValue.replace(/[^a-zA-Z0-9-. ]/g, '')].sort().join('');
const result = sorted.replace(/^(\d+)(.*)/, '$2$1');
console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

You could check with isFinite for numbers and sort then the rest by their value.

const
    sort = string => string
        .split('')
        .sort((a, b) => isFinite(a) - isFinite(b) || a > b || -(a < b))
        .join('');

console.log(sort('asd5lk43fj6vc'));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!